package edu.pmfbl.mnr.sem.mcmatm.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import com.borland.dbswing.*;
import com.borland.dx.dataset.*;

import edu.pmfbl.mnr.sem.mcmatm.app.*;
import edu.pmfbl.mnr.sem.mcmatm.beans.*;

/**
 * <p>Title: MCMA Test Maker</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2009</p>
 *
 * <p>Company: KrndijaSoft</p>
 *
 * @author Ninoslav Krndija
 * @version 1.0
 */
public class OdgovorEditDialog extends JDialog //implements ListSelectionListener
{
  private AppDataModule appDataModule = AppDataModule.getDataModule();
  DataSet pDataSet, oDataSet;

  JPanel contentPanel = new JPanel();
  BorderLayout borderLayout1 = new BorderLayout();
  JPanel pitanjePanel = new JPanel();
  JPanel odgovorPanel = new JPanel();
  JPanel buttonPanel = new JPanel();
  BorderLayout borderLayout2 = new BorderLayout();
  JPanel odgovorInfoPanel = new JPanel();
  JPanel odgovorNavPanel = new JPanel();
  BorderLayout borderLayout3 = new BorderLayout();
  JdbMyNavToolBar odgovoriNavToolBar = new JdbMyNavToolBar();
  TableScrollPane tableScrollPane1 = new TableScrollPane();
  JdbTable odgovoriTable = new JdbTable();
  JdbLabel idLabel = new JdbLabel();
  JdbLabel nazivLabel = new JdbLabel();
  JdbLabel tekstpitanjaLabel = new JdbLabel();
  JdbTextField odgovorField = new JdbTextField();
  JdbCheckBox tacanCheckBox = new JdbCheckBox();
  JScrollPane jScrollPane1 = new JScrollPane();
  JLabel odgovorLabel = new JLabel();
  JLabel infoLabel = new JLabel();
  JdbTextArea infoText = new JdbTextArea();
  JButton prihvatiButton = new JButton();
  JButton odustaniButton = new JButton();
  GridBagLayout gridBagLayout1 = new GridBagLayout();

  public OdgovorEditDialog()
  {
    this(new Frame(), "Uređivanje odgovora", true);
  }

  public OdgovorEditDialog(Frame owner, String title, boolean modal)
  {
    super(owner, title, modal);
    try
    {
      //setDefaultCloseOperation(DISPOSE_ON_CLOSE);
      jbInit();
      setDataSet(pDataSet, oDataSet);
    }
    catch (Exception exception)
    {
      exception.printStackTrace();
    }
  }

  public void addNotify()
  {
    super.addNotify();
    this.pack();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = this.getPreferredSize();
    if (frameSize.height > screenSize.height)
      frameSize.height = screenSize.height;
    if (frameSize.width > screenSize.width)
      frameSize.width = screenSize.width;
    this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
  }

  private void jbInit() throws Exception
  {
    pDataSet = appDataModule.getPitanjaDataSet();
    oDataSet = appDataModule.getOdgovoriDataSet();

    contentPanel.setLayout(borderLayout1);
    contentPanel.setPreferredSize(new Dimension(502, 428));
    tacanCheckBox.setTextWithMnemonic("");
    getContentPane().add(contentPanel);

    idLabel.setFont(new java.awt.Font("Dialog", Font.BOLD, 14));
    idLabel.setHorizontalAlignment(SwingConstants.RIGHT);
    idLabel.setBounds(new Rectangle(8, 8, 35, 18));
    nazivLabel.setFont(new java.awt.Font("Dialog", Font.BOLD, 14));
    nazivLabel.setBounds(new Rectangle(50, 8, 340, 18));
    tekstpitanjaLabel.setBounds(new Rectangle(50, 28, 340, 15));
    pitanjePanel.setLayout(null);
    pitanjePanel.setPreferredSize(new Dimension(502, 50));

    odgovorLabel.setText("Odgovor:");
    odgovorLabel.setBounds(new Rectangle(8, 8, 52, 15));
    infoLabel.setText("Info:");
    infoLabel.setBounds(new Rectangle(258, 8, 34, 15));
    odgovorField.setNextFocusableComponent(tacanCheckBox);  // INFO ima neki drugi (noviji) nacin upravljanja stavljanja u fokus
    odgovorField.setFont(new java.awt.Font("Monospaced", Font.PLAIN, 12));
    odgovorField.setMargin(new Insets(1, 2, 2, 2));
    odgovorField.setBounds(new Rectangle(8, 25, 237, 21));
    tacanCheckBox.setNextFocusableComponent(infoText);
    tacanCheckBox.setBounds(new Rectangle(6, 50, 55, 23));
    jScrollPane1.setBounds(new Rectangle(258, 25, 237, 46));
    jScrollPane1.getViewport().add(infoText);
    infoText.setNextFocusableComponent(odgovorField);
    odgovorInfoPanel.setLayout(null);
    odgovorInfoPanel.setPreferredSize(new Dimension(1, 79));

    tableScrollPane1.getViewport().add(odgovoriTable);
    odgovoriNavToolBar.setButtonStateDitto(JdbMyNavToolBar.HIDDEN);
    odgovoriNavToolBar.setButtonStatePost(JdbNavToolBar.HIDDEN);
    odgovoriNavToolBar.setButtonStateCancel(JdbNavToolBar.HIDDEN);
    odgovoriNavToolBar.setButtonStateSave(JdbNavToolBar.HIDDEN);
    odgovoriNavToolBar.setButtonStateRefresh(JdbNavToolBar.HIDDEN);
    odgovoriNavToolBar.setButtonStateInsert(JdbNavToolBar.ENABLED);
    odgovoriNavToolBar.getFirstButton().setToolTipText("Prvi odgovor");
    odgovoriNavToolBar.getPriorButton().setToolTipText("Prethodni odgovor");
    odgovoriNavToolBar.getNextButton().setToolTipText("Sledeći odgovor");
    odgovoriNavToolBar.getLastButton().setToolTipText("Poslednji odgovor");
    odgovoriNavToolBar.getInsertButton().setToolTipText("Novi odgovor");
    odgovoriNavToolBar.getDeleteButton().setToolTipText("Briši odgovor");
    odgovoriNavToolBar.setFloatable(false);
    odgovoriNavToolBar.getInsertButton().removeActionListener(odgovoriNavToolBar);
    odgovoriNavToolBar.getInsertButton().addActionListener(new java.awt.event.ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            insertButton_actionPerformed(e);
          }
        });

    odgovoriTable.setPopupMenuEnabled(false);
    odgovoriTable.setRowHeaderVisible(false);
    odgovoriTable.setEditable(false);
    odgovorNavPanel.setLayout(borderLayout3);
    odgovorPanel.setLayout(borderLayout2);

    prihvatiButton.setText("Prihvati");
    prihvatiButton.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        prihvatiButton_actionPerformed(e);
      }
    });

    odustaniButton.setText("Odustani");
    odustaniButton.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        odustaniButton_actionPerformed(e);
      }
    });
    buttonPanel.setLayout(gridBagLayout1);
    buttonPanel.setPreferredSize(new Dimension(502, 40));

    this.setResizable(false);
    this.setSize(502, 428);

    contentPanel.add(pitanjePanel, java.awt.BorderLayout.NORTH);
    contentPanel.add(odgovorPanel, java.awt.BorderLayout.CENTER);
    contentPanel.add(buttonPanel, java.awt.BorderLayout.SOUTH);
    pitanjePanel.add(idLabel);
    pitanjePanel.add(nazivLabel);
    pitanjePanel.add(tekstpitanjaLabel);
    odgovorPanel.add(odgovorInfoPanel, java.awt.BorderLayout.NORTH);
    odgovorPanel.add(odgovorNavPanel, java.awt.BorderLayout.CENTER);
    odgovorInfoPanel.add(odgovorLabel);
    odgovorInfoPanel.add(odgovorField);
    odgovorInfoPanel.add(tacanCheckBox);
    odgovorInfoPanel.add(infoLabel);
    odgovorInfoPanel.add(jScrollPane1);
    odgovorNavPanel.add(odgovoriNavToolBar, java.awt.BorderLayout.NORTH);
    odgovorNavPanel.add(tableScrollPane1, java.awt.BorderLayout.CENTER);
    buttonPanel.add(odustaniButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
        , GridBagConstraints.CENTER, GridBagConstraints.NONE,
        new Insets(8, 2, 8, 8), 12, 4));
    buttonPanel.add(prihvatiButton, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
        , GridBagConstraints.CENTER, GridBagConstraints.NONE,
        new Insets(8, 330, 8, 2), 12, 4));
    odgovorField.requestFocus();
  }

  void insertButton_actionPerformed(ActionEvent e)
  {
    appDataModule.insertAnswer();
    odgovorField.requestFocus();
  }

  //  implements ListSelectionListener
/*  public void valueChanged(ListSelectionEvent e)
  {
  }*/

  public void setDataSet(DataSet p, DataSet o)
  {
    idLabel.setDataSet(p);
    nazivLabel.setDataSet(p);
    tekstpitanjaLabel.setDataSet(p);
    odgovorField.setDataSet(o);
    tacanCheckBox.setDataSet(o);
    infoText.setDataSet(o);
    odgovoriNavToolBar.setDataSet(o);
    odgovoriTable.setDataSet(o);

    idLabel.setColumnName("id");
    nazivLabel.setColumnName("naziv");
    tekstpitanjaLabel.setColumnName("tekstpitanja");
    odgovorField.setColumnName("odgovor");
    tacanCheckBox.setColumnName("tacan");
    infoText.setColumnName("info");
  }

  void this_windowClosing(WindowEvent e)
  {
    dispose();
  }

  void prihvatiButton_actionPerformed(ActionEvent e)
  {
    // ovdje kod
    /*if(appDataModule.nemaTacnihOdgovora())
    {
      Component component = this;
      JDialog dialog = null;
      while((component = component.getParent()) != null)
      {
        if(component instanceof JDialog)
        {
          dialog = (JDialog) component;
          break;
        }
      }
      if(dialog == null)
        dialog = new JDialog();
      MessageDialog message = new MessageDialog(dialog);
      message.setMessage("Svako pitanje mora imati bar jedan tačan odgovor.");
      message.setVisible(true);
    }
    else
    {*/
    if(appDataModule.getUnosenjePitanja())
    {
      if(appDataModule.postAnswer())
        dispose();
    }
    else  // INFO kada se brisu odgovori treba ih izbrisati iz izvora podataka
      if(appDataModule.postAnswer())
      {
        dispose();
        if(appDataModule.getOdgovoriDataSet().getRowCount() == 0)
          appDataModule.deleteQuestion();
        appDataModule.saveQuestion();
      }
    //}
  }

  void odustaniButton_actionPerformed(ActionEvent e)
  {
    // ovdje kod
    if(oDataSet.isEditing())
      oDataSet.cancel();
    dispose();
  }

}

class OdgovorEditDialog_this_windowAdapter extends WindowAdapter
{
  OdgovorEditDialog adaptee;

  OdgovorEditDialog_this_windowAdapter(OdgovorEditDialog adaptee)
  {
    this.adaptee = adaptee;
  }

  public void windowClosing(WindowEvent e)
  {
    adaptee.this_windowClosing(e);
  }
}
